<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>每日一面--数据库连接池 | Celts</title><meta name="author" content="PaulGeorge"><meta name="copyright" content="PaulGeorge"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="每天一篇面试小知识  本篇着重介绍一下数据库连接池   写在前面 平时在使用 SpringBoot 框架结合 MyBatis 的时候有注意到数据库连接池吗？ 还真的没有过呀，还不是因为平时做的练习项目都是只注重使用而忽略了其更深层次的原理。 哎！拖欠的知识终究是要花时间补回来的，顺便回顾一下吧！">
<meta property="og:type" content="article">
<meta property="og:title" content="每日一面--数据库连接池">
<meta property="og:url" content="https://curry3035.gitee.io/posts/38677/index.html">
<meta property="og:site_name" content="Celts">
<meta property="og:description" content="每天一篇面试小知识  本篇着重介绍一下数据库连接池   写在前面 平时在使用 SpringBoot 框架结合 MyBatis 的时候有注意到数据库连接池吗？ 还真的没有过呀，还不是因为平时做的练习项目都是只注重使用而忽略了其更深层次的原理。 哎！拖欠的知识终究是要花时间补回来的，顺便回顾一下吧！">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://curry3035.gitee.io/img/avatar.jpg">
<meta property="article:published_time" content="2021-08-13T13:00:00.000Z">
<meta property="article:modified_time" content="2022-05-26T18:16:44.013Z">
<meta property="article:author" content="PaulGeorge">
<meta property="article:tag" content="进阶">
<meta property="article:tag" content="druid">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://curry3035.gitee.io/img/avatar.jpg"><link rel="shortcut icon" href="/img/ic.ico"><link rel="canonical" href="https://curry3035.gitee.io/posts/38677/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":false,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: PaulGeorge","link":"链接: ","source":"来源: Celts","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#1f1f1f","position":"top-right"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '每日一面--数据库连接池',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-05-27 02:16:44'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/background.css"><link rel="stylesheet" href="/css/my.css"><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Celts"><span class="site-name">Celts</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-archive"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-folder-open"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/photos/"><i class="fa-fw fa fa-camera-retro"></i><span> 图库</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">每日一面--数据库连接池</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-08-13T13:00:00.000Z" title="发表于 2021-08-13 21:00:00">2021-08-13</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-05-26T18:16:44.013Z" title="更新于 2022-05-27 02:16:44">2022-05-27</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E9%9D%A2%E8%AF%95%E7%AF%87/">面试篇</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>10分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="每日一面--数据库连接池"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><blockquote>
<p>每天一篇面试小知识</p>
</blockquote>
<p><strong>本篇着重介绍一下数据库连接池</strong></p>
<hr>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-15_04-35-37.png"></p>
<p><strong>写在前面</strong></p>
<p><font color=#008000>平时在使用 SpringBoot 框架结合 MyBatis 的时候有注意到数据库连接池吗？</font></p>
<p>还真的没有过呀，还不是因为平时做的练习项目都是只注重使用而忽略了其更深层次的原理。</p>
<p>哎！拖欠的知识终究是要花时间补回来的，顺便回顾一下吧！</p>
<span id="more"></span>



<h3 id="什么是数据库连接池？"><a href="#什么是数据库连接池？" class="headerlink" title="什么是数据库连接池？"></a>什么是数据库连接池？</h3><p>官方：数据库连接池（Connection pooling）是程序启动时建立足够的数据库连接，并将这些连接组成一个连接池，由程序动态地对池中的连接进行申请，使用，释放。</p>
<p>个人理解：就像是 Java 中的线程池一样，创建数据库连接是一个很耗时的操作，也容易对数据库造成安全隐患。所以在程序初始化的时候集中创建数据库连接池，负责分配、管理和释放数据库连接，它允许应用程序重复使用一个现有的数据库连接，而不是再重新建立一个。</p>
<br/>



<h3 id="为什么要使用连接池？"><a href="#为什么要使用连接池？" class="headerlink" title="为什么要使用连接池？"></a>为什么要使用连接池？</h3><p> 数据库连接是一种关键的有限的昂贵的资源，在多用户的网页应用程序中体现得尤为突出。 一个数据库连接对象均对应一个物理数据库连接，每次操作都打开一个物理连接，使用完都关闭连接，这样造成系统的 性能低下。</p>
<p>触类旁通的参照 Java 中的线程池，在一个“池”里放了好多半成品的数据库联接对象，由应用程序动态地对池中的连接进行申请、使用和释放。对于多于连接池中连接数的并发请求，应该在请求队列中排队等待。并且应用程序可以根据池中连接的使用率，动态增加或减少池中的连接数。</p>
<p>好处：</p>
<p>连接池技术尽可能多地重用了消耗内存地资源，大大节省了内存，提高了服务器地服务效率，能够支持更多的客户服务。通过使用连接池，将大大提高程序运行效率，</p>
<br/>

<h3 id="与传统的连接机制区别"><a href="#与传统的连接机制区别" class="headerlink" title="与传统的连接机制区别"></a>与传统的连接机制区别</h3><h4 id="不使用连接池流程"><a href="#不使用连接池流程" class="headerlink" title="不使用连接池流程"></a>不使用连接池流程</h4><p><strong>举个荔枝：</strong>执行一个 SQL 命令，如果不使用连接池，需要经过哪些流程【MySQL 为例】</p>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-14_21-19-12.png"></p>
<p><strong>不使用数据库连接池的步骤：</strong></p>
<ol>
<li>TCP 建立连接的三次握手</li>
<li>MySQL 认证的三次握手</li>
<li>真正的 SQ L执行</li>
<li>MySQL 的关闭</li>
<li>TCP 的四次握手关闭</li>
</ol>
<p><strong>优点：实现简单</strong></p>
<p><strong>缺点：</strong></p>
<ol>
<li>网络 IO 较多</li>
<li>数据库的负载较高</li>
<li>响应时间较长及 QPS 较低</li>
<li>应用频繁的创建连接和关闭连接，导致临时对象较多，GC 频繁</li>
<li>在关闭连接后，会出现大量 TIME_WAIT 的TCP 状态（在2个MSL之后关闭）</li>
</ol>
<h4 id="使用连接池流程"><a href="#使用连接池流程" class="headerlink" title="使用连接池流程"></a>使用连接池流程</h4><p><strong>举个荔枝：</strong>执行一个 SQL 命令，如果使用连接池，只需要经过哪些流程【MySQL 为例】</p>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-14_21-28-29.png"></p>
<p><strong>使用数据库连接池的步骤：</strong></p>
<ol>
<li><p>第一次访问的时候，需要建立连接</p>
</li>
<li><p>之后的访问，均会<strong>复用</strong>之前创建的连接，直接执行 SQL 语句。</p>
</li>
</ol>
<p><strong>优点：</strong></p>
<ol>
<li><p>较少了网络开销</p>
</li>
<li><p>系统的性能会有一个实质的提升</p>
</li>
<li><p>没了麻烦的 TIME_WAIT 状态</p>
</li>
</ol>
<p><strong>连接池解决现状问题的原理</strong></p>
<table>
<thead>
<tr>
<th>Connection连接对象</th>
<th>操作特点</th>
</tr>
</thead>
<tbody><tr>
<td>创建时</td>
<td>连接对象不再由自己创建，而是系统启动的时候已经创建一定数量的连接， 并且放在连接池中</td>
</tr>
<tr>
<td>使用时</td>
<td>直接从连接池中去获取一个已经创建好的连接对象即可</td>
</tr>
<tr>
<td>关闭时</td>
<td>不是真的关闭连接对象，而是将连接对象再放回到连接池中，供下一个用户使用</td>
</tr>
</tbody></table>
<br/>



<h3 id="数据库连接池的工作原理"><a href="#数据库连接池的工作原理" class="headerlink" title="数据库连接池的工作原理"></a>数据库连接池的工作原理</h3><h4 id="三部分组成"><a href="#三部分组成" class="headerlink" title="三部分组成"></a>三部分组成</h4><ol>
<li>连接池的建立</li>
<li>连接池中连接的使用管理</li>
<li>连接池的关闭</li>
</ol>
<p><strong>举个荔枝：</strong>使用连接池【MySQL 为例】</p>
<img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-15_04-32-29.png" style="zoom:90%;" />



<ol>
<li>系统在启动时初始化连接池；</li>
<li>向连接池请求可用的数据库连接；</li>
<li>如果没有获取到可用的数据库连接，并且连接池中连接的数量小于最大连接数，则按照规定的步长给连接池中添加连接，然后再获取，如果连接池中的数量已经到了最大连接数还没有获取到可用的连接，则等待其他请求释放了连接后再获取；</li>
<li>使用获取到的数据库连接请求数据库；</li>
<li>将数据库连接放回连接池，供其他连接使用；</li>
</ol>
<h4 id="连接池主要参数"><a href="#连接池主要参数" class="headerlink" title="连接池主要参数"></a>连接池主要参数</h4><p><strong>使用连接池时，要配置一下参数</strong></p>
<ol>
<li>最小连接数【是连接池一直保持的数据库连接,所以如果应用程序对数据库连接的使用量不大,将会有大量的数据库连接资源被浪费】</li>
<li>最大连接数【是连接池能申请的最大连接数,如果数据库连接请求超过次数,后面的数据库连接请求将被加入到等待队列中,这会影响以后的数据库操作】</li>
<li>最大空闲时间【如果一个连接对象长时间没有人使用，设置多久回收这个对象，默认是不回收】</li>
<li>获取连接超时时间【如果连接池中没有连接对象，设置用户等待的最长时间是多久，单位是毫秒。<br>如果超过这个时间就抛出异常】</li>
<li>超时重试连接次数</li>
</ol>
<h4 id="连接池需要注意的点"><a href="#连接池需要注意的点" class="headerlink" title="连接池需要注意的点"></a>连接池需要注意的点</h4><ul>
<li>并发问题</li>
<li>事务处理</li>
<li>连接池的分配与释放</li>
<li>连接池的配置与维护</li>
</ul>
<h4 id="数据库对比"><a href="#数据库对比" class="headerlink" title="数据库对比"></a>数据库对比</h4><p><strong>第一、二代连接池</strong></p>
<table>
<thead>
<tr>
<th><strong>数据库连接池</strong></th>
<th><strong>最新版本</strong></th>
<th><strong>发布时间</strong></th>
</tr>
</thead>
<tbody><tr>
<td>C3P0</td>
<td>c3p0-0.9.5.2</td>
<td>on 9 Dec 2015</td>
</tr>
<tr>
<td>DBCP</td>
<td>2.2.0</td>
<td>27 December 2017</td>
</tr>
<tr>
<td><strong>Druid</strong></td>
<td>0.11.0</td>
<td>Dec 4 2017</td>
</tr>
<tr>
<td><strong>HikariCP</strong></td>
<td>2.7.6</td>
<td>2018-01-14</td>
</tr>
</tbody></table>
<br/>



<h3 id="Druid连接池"><a href="#Druid连接池" class="headerlink" title="Druid连接池"></a>Druid连接池</h3><h4 id="Druid-简介"><a href="#Druid-简介" class="headerlink" title="Druid 简介"></a>Druid 简介</h4><blockquote>
<p>Druid是阿里巴巴开发的号称为监控而生的数据库连接池，在功能、性能、扩展性方面，都超过其他数据库连接池。Druid已经在阿里巴巴部署了超过600个应用，经过一年多生产环境大规模部署的严苛考验。如：一年一度的双十一活动，每年春运的抢火车票。</p>
</blockquote>
<h4 id="Druid"><a href="#Druid" class="headerlink" title="Druid"></a>Druid</h4><ul>
<li>是一个阿里开源的连接池组件</li>
<li>加⼊了日志监控，不需要额外的界面开发</li>
</ul>
<h4 id="Druid-常用的配置参数"><a href="#Druid-常用的配置参数" class="headerlink" title="Druid 常用的配置参数"></a>Druid 常用的配置参数</h4><table>
<thead>
<tr>
<th align="center">参数</th>
<th align="center"><strong>缺省值</strong></th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">url</td>
<td align="center"></td>
<td align="center">连接字符串</td>
</tr>
<tr>
<td align="center">username</td>
<td align="center"></td>
<td align="center">用户名</td>
</tr>
<tr>
<td align="center">password</td>
<td align="center"></td>
<td align="center">密码</td>
</tr>
<tr>
<td align="center">driverClassName</td>
<td align="center"></td>
<td align="center">驱动类名，会自动根据URL识别，这一项可以不配置</td>
</tr>
<tr>
<td align="center">initialSize</td>
<td align="center">0</td>
<td align="center">初始连接数</td>
</tr>
<tr>
<td align="center">maxActive</td>
<td align="center">8</td>
<td align="center">最大连接池数量</td>
</tr>
<tr>
<td align="center">minIdle</td>
<td align="center"></td>
<td align="center">最小连接池数量</td>
</tr>
<tr>
<td align="center">maxWait</td>
<td align="center"></td>
<td align="center">最长等待时间，单位毫秒</td>
</tr>
</tbody></table>
<h4 id="Druid-连接池运行原理"><a href="#Druid-连接池运行原理" class="headerlink" title="Druid 连接池运行原理"></a>Druid 连接池运行原理</h4><ol>
<li><p>连接池初始化的时候按照 initialSize 创建多个连接【默认 0 个】</p>
</li>
<li><p>有 DB 操作访问的时候，就从里面取一个 【类似线程池的 task 任务】</p>
</li>
<li><p>如果当前正在使用的连接 = maxActive ,就会进入等待，没有到 maxActive 拿一个空闲连接，没有空闲就创建一个新连接，等待超过 maxWati 则会报错【最大连接池数量为阈值来决定是否创建新的连接或者报错】</p>
</li>
<li><p>使用完毕还回去等待其它人用，不会物理销毁【持续存在】</p>
</li>
<li><p>每一个 connection 在连接池里都有空闲时长的，允许最大空闲时长：minEvictableldleTimeMillis，多久检测一次：timeBetweenEvictionRunsMillis</p>
</li>
<li><p>maxActive 如何配置：理论上应该设置成最大并发数 </p>
</li>
<li><p>只要连接被前端业务拿到就算不空闲了，这个时候如果说长时间占有连接，连接数是不会崩溃，连接池主动关闭是使用中的连接，属于一个高级功能</p>
</li>
</ol>
<figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="attr">removeAbandoned</span>=<span class="string">“true”</span></span><br><span class="line"><span class="attr">removeAbandonedTimeout</span>=<span class="string">“60”</span></span><br><span class="line"><span class="attr">logAbandoned</span>=<span class="string">“true”</span></span><br></pre></td></tr></table></figure>
<br/>



<h3 id="Druid-连接池-SpringBoot-集成"><a href="#Druid-连接池-SpringBoot-集成" class="headerlink" title="Druid 连接池 SpringBoot 集成"></a>Druid 连接池 SpringBoot 集成</h3><h4 id="导入依赖"><a href="#导入依赖" class="headerlink" title="导入依赖"></a>导入依赖</h4><figure class="highlight xml"><table><tr><td class="code"><pre><span class="line"><span class="comment">&lt;!-- mysql --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.49<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- mybatis-plus --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.baomidou<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-plus-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.2.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- druid --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1.21<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>



<h4 id="编写配置文件"><a href="#编写配置文件" class="headerlink" title="编写配置文件"></a>编写配置文件</h4><figure class="highlight yaml"><table><tr><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">datasource:</span></span><br><span class="line">    <span class="attr">druid:</span></span><br><span class="line">      <span class="attr">username:</span> <span class="string">root</span></span><br><span class="line">      <span class="attr">password:</span> <span class="string">root</span></span><br><span class="line">      <span class="attr">url:</span> <span class="string">jdbc:mysql://localhost:3306/mybatis-plus?useUnicode=true&amp;characterEncoding=UTF-8</span></span><br><span class="line">      <span class="attr">db-type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line">      <span class="attr">driver-class-name:</span> <span class="string">com.mysql.jdbc.Driver</span></span><br><span class="line">      <span class="attr">initial-size:</span> <span class="number">5</span></span><br><span class="line">      <span class="attr">min-idle:</span> <span class="number">5</span></span><br><span class="line">      <span class="attr">max-active:</span> <span class="number">20</span></span><br><span class="line">      <span class="attr">max-wait:</span> <span class="number">60000</span></span><br><span class="line">      <span class="attr">time-between-eviction-runs-millis:</span> <span class="number">60000</span></span><br><span class="line">      <span class="attr">min-evictable-idle-time-millis:</span> <span class="number">300000</span></span><br><span class="line">      <span class="attr">validation-query:</span> <span class="string">SELECT</span> <span class="number">1</span> <span class="string">FROM</span> <span class="string">DUAL</span></span><br><span class="line">      <span class="attr">test-while-idle:</span> <span class="literal">true</span></span><br><span class="line">      <span class="attr">test-on-borrow:</span> <span class="literal">false</span></span><br><span class="line">      <span class="attr">test-on-return:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment"># 打开PSCache</span></span><br><span class="line">      <span class="attr">pool-prepared-statements:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment">#配置监控统计拦截的filters，stat:监控统计、log4j：日志记录、wall：防御sql注入</span></span><br><span class="line">      <span class="comment">#如果运行时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority</span></span><br><span class="line">      <span class="comment">#则导入 log4j 依赖即可，Maven 地址： https://mvnrepository.com/artifact/log4j/log4j</span></span><br><span class="line">      <span class="attr">filter:</span> <span class="string">stat,wall,log4j,config</span></span><br><span class="line">      <span class="comment">#指定每个连接上PSCache的大小</span></span><br><span class="line">      <span class="attr">max-pool-prepared-statement-per-connection-size:</span> <span class="number">20</span></span><br><span class="line">      <span class="comment">#合并多个DruidDataSource的监控数据</span></span><br><span class="line">      <span class="attr">use-global-data-source-stat:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment">#通过connectProperties属性来打开mergeSql功能；慢SQL记录</span></span><br><span class="line">      <span class="attr">connection-properties:</span> <span class="string">druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000</span></span><br></pre></td></tr></table></figure>



<h4 id="编写配置类"><a href="#编写配置类" class="headerlink" title="编写配置类"></a>编写配置类</h4><p><strong>Druid数据源监控</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DruidConfig</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@ConfigurationProperties(prefix = &quot;spring.datasource&quot;)</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> DataSource <span class="title function_">druidDataSource</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">DruidDataSource</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//因为Springboot内置了servlet容器，所以没有web.xml，替代方法就是将ServletRegistrationBean注册进去</span></span><br><span class="line">    <span class="comment">//加入后台监控</span></span><br><span class="line">    <span class="comment">//这里其实就相当于servlet的web.xml</span></span><br><span class="line">    <span class="meta">@Bean</span>  </span><br><span class="line">    <span class="keyword">public</span> ServletRegistrationBean <span class="title function_">statViewServlet</span><span class="params">()</span>&#123;</span><br><span class="line">        ServletRegistrationBean&lt;StatViewServlet&gt; bean = <span class="keyword">new</span> <span class="title class_">ServletRegistrationBean</span>&lt;&gt;(<span class="keyword">new</span> <span class="title class_">StatViewServlet</span>(),<span class="string">&quot;/druid/*&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//后台需要有人登录，进行配置</span></span><br><span class="line">        <span class="comment">//bean.addUrlMappings(); 这个可以添加映射，我们在构造里已经写了</span></span><br><span class="line">        <span class="comment">//设置一些初始化参数</span></span><br><span class="line">        Map&lt;String,String&gt; initParas = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String,String&gt;();</span><br><span class="line">        initParas.put(<span class="string">&quot;loginUsername&quot;</span>,<span class="string">&quot;admin&quot;</span>);<span class="comment">//它这个账户密码是固定的</span></span><br><span class="line">        initParas.put(<span class="string">&quot;loginPassword&quot;</span>,<span class="string">&quot;123456&quot;</span>);</span><br><span class="line">        <span class="comment">//允许谁能防伪</span></span><br><span class="line">        initParas.put(<span class="string">&quot;allow&quot;</span>,<span class="string">&quot;&quot;</span>);<span class="comment">//这个值为空或没有就允许所有人访问，ip白名单</span></span><br><span class="line">        <span class="comment">//initParas.put(&quot;allow&quot;,&quot;localhost&quot;);//只允许本机访问，多个ip用逗号,隔开</span></span><br><span class="line">        <span class="comment">//initParas.put(&quot;deny&quot;,&quot;&quot;);//ip黑名单，拒绝谁访问 deny和allow同时存在优先deny</span></span><br><span class="line">        initParas.put(<span class="string">&quot;resetEnable&quot;</span>,<span class="string">&quot;false&quot;</span>);<span class="comment">//禁用HTML页面的Reset按钮</span></span><br><span class="line">        bean.setInitParameters(initParas);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//再配置一个过滤器，Servlet按上面的方式注册Filter也只能这样</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="keyword">public</span> FilterRegistrationBean <span class="title function_">webStatFilter</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="type">FilterRegistrationBean</span> <span class="variable">bean</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">FilterRegistrationBean</span>();</span><br><span class="line">        <span class="comment">//可以设置也可以获取,设置一个阿里巴巴的过滤器</span></span><br><span class="line">        bean.setFilter(<span class="keyword">new</span> <span class="title class_">WebStatFilter</span>());</span><br><span class="line">        bean.addUrlPatterns(<span class="string">&quot;/*&quot;</span>);</span><br><span class="line">        <span class="comment">//可以过滤和排除哪些东西</span></span><br><span class="line">        Map&lt;String,String&gt; initParams = <span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;String,String&gt;();</span><br><span class="line">        <span class="comment">//把不需要监控的过滤掉,这些不进行统计</span></span><br><span class="line">        initParams.put(<span class="string">&quot;exclusions&quot;</span>,<span class="string">&quot;*.js,*.css,/druid/*&quot;</span>);</span><br><span class="line">        bean.setInitParameters(initParams);</span><br><span class="line">        <span class="keyword">return</span> bean;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>



<h4 id="启动项目"><a href="#启动项目" class="headerlink" title="启动项目"></a>启动项目</h4><p><strong>访问 URL：<a target="_blank" rel="noopener" href="http://localhost:8080/druid/login.html">http://localhost:8080/druid/login.html</a></strong></p>
<p>就会到Druid的监控登录界面</p>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-15_03-46-55.png"></p>
<p>输入我们配置的登录用户名和密码进行登录即可访问</p>
<p><img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-15_03-47-26.png"></p>
<h4 id="PSCache"><a href="#PSCache" class="headerlink" title="PSCache"></a>PSCache</h4><ul>
<li>PreparedStatement Cache 【准备语句缓存】</li>
<li>就要从 MySQL 的 SQL 执⾏行行过程来讲</li>
</ul>
<img src="https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@master/img/all/Snipaste_2021-08-15_04-01-13.png" style="zoom: 90%;" />

<p><strong>名词解释</strong></p>
<ol>
<li><p>连接器【进⾏行行数据库连接，⽤用户名密码验证】</p>
</li>
<li><p>查询缓存【key-value， key 是 SQL， value 就是结果集】</p>
</li>
</ol>
<figure class="highlight properties"><table><tr><td class="code"><pre><span class="line"><span class="attr">testOnBorrow</span>: <span class="string">false</span></span><br><span class="line"><span class="attr">testOnReturn</span>: <span class="string">false</span></span><br><span class="line"><span class="attr">poolPreparedStatements</span>: <span class="string">false</span></span><br><span class="line"></span><br><span class="line"><span class="attr">query_cache_type</span> : <span class="string">DEMAND //默认的SQL都不不使⽤用查询缓存</span></span><br><span class="line"><span class="attr">mysql&gt;</span> <span class="string">select SQL_CACHE * from student where id=1; //声明使⽤用</span></span><br><span class="line">  </span><br></pre></td></tr></table></figure>

<ol start="3">
<li>分析器</li>
</ol>
<ul>
<li>词法分析【SQL的关键词进⾏行行识别】</li>
<li>语法分析【对语法进⾏行行分析 “use near”】</li>
</ul>
<ol start="4">
<li><p>优化器器【决定我们SQL使⽤用那个⼈人索引】</p>
</li>
<li><p>执⾏器【会判断你是否对这个表有查询/更更新权限】</p>
</li>
<li><p>存储引擎【获取数据的具体⽂文件地址】</p>
</li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io">PaulGeorge</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://curry3035.gitee.io/posts/38677/">https://curry3035.gitee.io/posts/38677/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://curry3035.gitee.io" target="_blank">Celts</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E8%BF%9B%E9%98%B6/">进阶</a><a class="post-meta__tags" href="/tags/druid/">druid</a></div><div class="post_share"><div class="social-share" data-image="/img/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/51171/" title="实践一下--MyBatisPlus"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">实践一下--MyBatisPlus</div></div></a></div><div class="next-post pull-right"><a href="/posts/8859/" title="实践一下--Log日志"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">实践一下--Log日志</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/posts/55119/" title="每日一面--Files工具类"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-01-01</div><div class="title">每日一面--Files工具类</div></div></a></div><div><a href="/posts/34600/" title="面试一下--JUC入门"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-09-10</div><div class="title">面试一下--JUC入门</div></div></a></div><div><a href="/posts/11315/" title="实践一下--Spring Security"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-06-01</div><div class="title">实践一下--Spring Security</div></div></a></div><div><a href="/posts/40042/" title="每日一面--反射"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-05-01</div><div class="title">每日一面--反射</div></div></a></div><div><a href="/posts/56511/" title="实践一下--MySQL 优化"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-01-04</div><div class="title">实践一下--MySQL 优化</div></div></a></div><div><a href="/posts/27951/" title="学习--ElasticSearch"><div class="cover" style="background: var(--default-bg-color)"></div><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-09-29</div><div class="title">学习--ElasticSearch</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">PaulGeorge</div><div class="author-info__description">很多事情就像是旅行一样，当你决定要出发的时候，最困难的那部分其实就已经完成了</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">97</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">64</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">25</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/PaulGeorge123"><i class="fab fa-github"></i><span>GitHub URL</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/PaulGeorge123" target="_blank" title="Github"><i class="fab fa-github"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%EF%BC%9F"><span class="toc-number">1.</span> <span class="toc-text">什么是数据库连接池？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E4%BD%BF%E7%94%A8%E8%BF%9E%E6%8E%A5%E6%B1%A0%EF%BC%9F"><span class="toc-number">2.</span> <span class="toc-text">为什么要使用连接池？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8E%E4%BC%A0%E7%BB%9F%E7%9A%84%E8%BF%9E%E6%8E%A5%E6%9C%BA%E5%88%B6%E5%8C%BA%E5%88%AB"><span class="toc-number">3.</span> <span class="toc-text">与传统的连接机制区别</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8D%E4%BD%BF%E7%94%A8%E8%BF%9E%E6%8E%A5%E6%B1%A0%E6%B5%81%E7%A8%8B"><span class="toc-number">3.1.</span> <span class="toc-text">不使用连接池流程</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%BF%9E%E6%8E%A5%E6%B1%A0%E6%B5%81%E7%A8%8B"><span class="toc-number">3.2.</span> <span class="toc-text">使用连接池流程</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5%E6%B1%A0%E7%9A%84%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-number">4.</span> <span class="toc-text">数据库连接池的工作原理</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%89%E9%83%A8%E5%88%86%E7%BB%84%E6%88%90"><span class="toc-number">4.1.</span> <span class="toc-text">三部分组成</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E6%B1%A0%E4%B8%BB%E8%A6%81%E5%8F%82%E6%95%B0"><span class="toc-number">4.2.</span> <span class="toc-text">连接池主要参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%9E%E6%8E%A5%E6%B1%A0%E9%9C%80%E8%A6%81%E6%B3%A8%E6%84%8F%E7%9A%84%E7%82%B9"><span class="toc-number">4.3.</span> <span class="toc-text">连接池需要注意的点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AF%B9%E6%AF%94"><span class="toc-number">4.4.</span> <span class="toc-text">数据库对比</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Druid%E8%BF%9E%E6%8E%A5%E6%B1%A0"><span class="toc-number">5.</span> <span class="toc-text">Druid连接池</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Druid-%E7%AE%80%E4%BB%8B"><span class="toc-number">5.1.</span> <span class="toc-text">Druid 简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Druid"><span class="toc-number">5.2.</span> <span class="toc-text">Druid</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Druid-%E5%B8%B8%E7%94%A8%E7%9A%84%E9%85%8D%E7%BD%AE%E5%8F%82%E6%95%B0"><span class="toc-number">5.3.</span> <span class="toc-text">Druid 常用的配置参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Druid-%E8%BF%9E%E6%8E%A5%E6%B1%A0%E8%BF%90%E8%A1%8C%E5%8E%9F%E7%90%86"><span class="toc-number">5.4.</span> <span class="toc-text">Druid 连接池运行原理</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Druid-%E8%BF%9E%E6%8E%A5%E6%B1%A0-SpringBoot-%E9%9B%86%E6%88%90"><span class="toc-number">6.</span> <span class="toc-text">Druid 连接池 SpringBoot 集成</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E4%BE%9D%E8%B5%96"><span class="toc-number">6.1.</span> <span class="toc-text">导入依赖</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">6.2.</span> <span class="toc-text">编写配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BC%96%E5%86%99%E9%85%8D%E7%BD%AE%E7%B1%BB"><span class="toc-number">6.3.</span> <span class="toc-text">编写配置类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E9%A1%B9%E7%9B%AE"><span class="toc-number">6.4.</span> <span class="toc-text">启动项目</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#PSCache"><span class="toc-number">6.5.</span> <span class="toc-text">PSCache</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/47231/" title="POI读取Excel问题">POI读取Excel问题</a><time datetime="2023-04-11T01:00:00.000Z" title="发表于 2023-04-11 09:00:00">2023-04-11</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/8422/" title="Excel大文件的上传">Excel大文件的上传</a><time datetime="2023-04-10T01:00:00.000Z" title="发表于 2023-04-10 09:00:00">2023-04-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/55119/" title="每日一面--Files工具类">每日一面--Files工具类</a><time datetime="2023-01-01T01:00:00.000Z" title="发表于 2023-01-01 09:00:00">2023-01-01</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/34600/" title="面试一下--JUC入门">面试一下--JUC入门</a><time datetime="2022-09-10T01:00:00.000Z" title="发表于 2022-09-10 09:00:00">2022-09-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/16284/" title="每日一面--字符流和字节流">每日一面--字符流和字节流</a><time datetime="2022-07-01T01:00:00.000Z" title="发表于 2022-07-01 09:00:00">2022-07-01</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gcore.jsdelivr.net/gh/PaulGeorge123/cloudimg@img/mig2023/background05.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By PaulGeorge</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></body></html>